<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Iterator</title>
  </head>
  <body>
    <div class="container">
      <p>一段文字</p>
      <p>一段文字</p>
    </div>
    <div class="container">
      <p>一段文字</p>
      <p>一段文字</p>
    </div>
    <script>
      function* traverse(elemList) {
        for (const ele of elemList) {
          yield ele;
          const children = Array.from(ele.children);
          if (children.length) {
            yield* traverse(children);
          }
        }
      }
      const container = document.getElementsByClassName("container");
      for (let node of traverse(container)) {
        console.log(node);
      }
    </script>
  </body>
</html>
